package com.tengju.bff.interfaces.manage.crm.dashborad.facade;

import com.tengju.bff.interfaces.manage.crm.dashborad.vo.SecPullingAnalysisVO;
import com.tengju.bff.interfaces.shared.*;
import com.tengju.data.domain.model.clickhouse.CrmSecPullingAnalysisRepository;
import com.tengju.data.domain.model.clickhouse.DwdUserClubMapRepository;
import com.tengju.data.domain.model.clickhouse.LevelUserAnalysis;
import com.tengju.data.domain.model.clickhouse.UserPulling;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.shared.Quarter;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/3/19
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SecPullingAnalysisFacade {

    private final DwdUserClubMapRepository userClubMapRepository;
    private final CrmSecPullingAnalysisRepository crmSecPullingAnalysisRepository;
    private final UserInfoRepository userInfoRepository;

    public SecPullingAnalysisVO secPullingAnalysis(Long clubId) {
        SecPullingAnalysisVO secPullingAnalysisVO = new SecPullingAnalysisVO();
        List<LevelUserAnalysis> levelUserAnalyses = userClubMapRepository.listLevelUsers(clubId);
        Map<StarLevelEnum,Integer> levelUserMap = new EnumMap<>(StarLevelEnum.class);
        if (!CollectionUtils.isEmpty(levelUserAnalyses)) {
            levelUserMap = levelUserAnalyses.stream()
                    .collect(Collectors.toMap(LevelUserAnalysis::getStarLevel, LevelUserAnalysis::getCount,(v1, v2) -> v1));
        }
        List<KVVO> header = List.of(
                KVVO.of(StarLevelEnum.NONE, levelUserMap.getOrDefault(StarLevelEnum.NONE,0)),
                KVVO.of(StarLevelEnum.CITY_STAR_ONE, levelUserMap.getOrDefault(StarLevelEnum.CITY_STAR_ONE,0)),
                KVVO.of(StarLevelEnum.CITY_STAR_TWO, levelUserMap.getOrDefault(StarLevelEnum.CITY_STAR_TWO,0)),
                KVVO.of(StarLevelEnum.CITY_STAR_THREE, levelUserMap.getOrDefault(StarLevelEnum.CITY_STAR_THREE,0)),
                KVVO.of(StarLevelEnum.CITY_STAR_FOUR, levelUserMap.getOrDefault(StarLevelEnum.CITY_STAR_FOUR,0)),
                KVVO.of(StarLevelEnum.EC, levelUserMap.getOrDefault(StarLevelEnum.EC,0)));
        secPullingAnalysisVO.setHeader(header);
        secPullingAnalysisVO.setTable(secPullingTable(clubId,StarLevelEnum.NONE));
        return secPullingAnalysisVO;
    }

    public TableVO secPullingTable(Long clubId, StarLevelEnum starLevel) {
        if (starLevel == null) {
            return null;
        }
        LocalDate now = LocalDate.now();
        Quarter quarter = new Quarter(now);
        List<UserPulling> userPullingList = crmSecPullingAnalysisRepository.listSecBlackPulling(clubId, quarter,starLevel);
        if (CollectionUtils.isEmpty(userPullingList)) {
            return null;
        }
        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("showName", "用户", false),
                ColumnVO.of("allCount", "总数", true),
                ColumnVO.of("qCount", "本季度新增", true),
                ColumnVO.of("mCount", "本月新增", true),
                ColumnVO.of("tdCount", "今日新增", true));
        table.setHeader(columns);

        List<com.tengju.user.domain.model.user.UserIdCode> queryIdCode = userPullingList.stream()
                .map(v -> UserIdCode.of(v.getIdCode().getValue()))
                .collect(Collectors.toList());
        List<UserInfo> userInfos = userInfoRepository.batchGetUserInfo(queryIdCode);
        Map<Long, UserInfo> userInfoMap = userInfos.stream()
                .collect(Collectors.toMap(k -> k.getIdCode().getValue(), v -> v, (v1, v2) -> v1));
        List<Map<String, Object>> rows = new ArrayList<>();
        for (UserPulling userPulling : userPullingList) {
            Map<String, Object> row = new HashMap<>();
            UserInfo userInfo = userInfoMap.get(userPulling.getIdCode().getValue());
            StarLevelEnum starLevelEnum = StarLevelEnum.parseByStarLevel(userInfo.getStarLevel());
            String showName = userInfo.getNickName();
            if (StringUtils.isEmpty(showName)) {
                showName = new MobileVO(userInfo.getWholePhoneNumber()).toString();
            }
            row.put("showName", showName + " " + starLevelEnum.getDesc());
            row.put("allCount",new AmountVO(AmountType.NONE,userPulling.getAllCount()));
            row.put("qCount",new AmountVO(AmountType.NONE,userPulling.getQCount()));
            row.put("mCount",new AmountVO(AmountType.NONE,userPulling.getMCount()));
            row.put("tdCount",new AmountVO(AmountType.NONE,userPulling.getTdCount()));
            row.put("idCode",userPulling.getIdCode().getValue());
            rows.add(row);
        }
        table.setRecords(rows);
        return table;
    }
}
